use super::*;

#[derive(Debug, Clone, Copy)]
pub struct Table<'a> {
	// fixed32	version	Version number of the font descriptors table (0x00010000 for the current version).
	pub version: u32,
	// uint32	descriptorCount	Number of style descriptors that will be included to characterize this font. Each descriptor consists of a <tag, value> pair. These pairs are located in the gxFontDescriptor array that follows.
	// gxFontDescriptor	descriptor[]	List of tagged-coordinate pairs.
	pub descriptor: LazyArray32<'a, Descriptor>,
}
impl<'a> FromSlice<'a> for Table<'a> {
	fn parse(data: &'a [u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		Some(Self {
			version: s.read()?,
			descriptor: s.parse_array32()?,
		})
	}
}

#[derive(Clone, Copy)]
pub struct Descriptor {
	// uint32	tag	The 4-byte table tag name.
	pub tag: Tag,
	// fixed32	value	The value for the descriptor tag.
	pub value: i32,
}
impl FromData for Descriptor {
	const SIZE: usize = 8;
	fn parse(data: &[u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		Some(Self {
			tag: s.read()?,
			value: s.read()?,
		})
	}
}
impl core::fmt::Debug for Descriptor {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let mut f = f.debug_struct("Descriptor");
		f.field("tag", &self.tag);
		if self.tag == Self::NON_ALPHABETIC {
			_ = f.field("value", &self.value);
		} else {
			_ = f.field("value", &Fixed::from_i32(self.value));
		}
		f.finish()
	}
}
impl Descriptor {
	// Weight wght 1.0 Percent weight relative to regular weight.
	pub const WEIGHT: Tag = Tag::from_bytes(b"wght");
	// Width wdth 1.0 Percent width relative to regular width.
	pub const WIDTH: Tag = Tag::from_bytes(b"wdth");
	// Slant slnt 0.0 Angle of slant in degrees, where positive is clockwise from straight up.
	pub const SLANT: Tag = Tag::from_bytes(b"slnt");
	// Optical size	opsz 12.0 Point size the font was designed for.
	pub const OPTICAL: Tag = Tag::from_bytes(b"opsz");
	// Non-alphabetic nalf 0 These values are treated as integers, not fixed32s. 0 means alphabetic, and greater integers mean the font is non-alphabetic (e.g. symbols).
	pub const NON_ALPHABETIC: Tag = Tag::from_bytes(b"nalf");

	// The non-alphabetic code values are:
	pub const ALPHABETIC: i32 = 0;
	pub const DINGBATS: i32 = 1;
	pub const PI_CHARACTERS: i32 = 2;
	pub const FLEURONS: i32 = 3;
	pub const DECORATIVE_BORDERS: i32 = 4;
	pub const INTERNATIONAL_SYMBOLS: i32 = 5;
	pub const MATH_SYMBOLS: i32 = 6;
	pub fn value(&self) -> Fixed {
		Fixed::from_i32(self.value)
	}
}
